<?php  namespace App\Api\Models;

use Input;
use App\Api\Models\User;
class Repair extends BaseModel{
    
	//表明
	protected $table = 'apply_repair';
	//主键
	protected $primaryKey = 'id';
    //申请保修
    public function AppRepair() {
        $mobile = Input::get('mobile');
        $eno = Input::get('eno');
        $bno = Input::get('bno');
        $text = Input::get('text');
        //
        if(!$mobile){
            $retArr["msg"]= "请传入手机号";
            $retArr["result"] = 0;
            return $retArr;
        }
        if(!$eno){
            $retArr["msg"]= "未获取到箱体号";
            $retArr["result"] = 0;
            return $retArr;
        }
        if(!$bno){
            $retArr["msg"]= "未获取到柜号";
            $retArr["result"] = 0;
            return $retArr;
        }
        $user = User::select('*')
                ->where('phone','=',$mobile)
                ->where('status','=',1)
                ->where('is_delete','=',0)
                ->first();
        if($user){
            $box = Cabinet::select('*')
                ->where('code','=',$eno)
                ->where('is_delete','=',0)
                ->first();
            if(!$box){
                $retArr["msg"]= "未获取到箱体号";
                $retArr["result"] = 0;
                return $retArr;
            }
            $cell = CabinetCell::select('*')
                    ->where('cabinet_id','=',$box->id)
                    ->where('code','=',$bno)
                    ->where('is_delete','=',0)
                    ->first();
            if(!$cell){
                $retArr["msg"]= "未获取到柜号";
                $retArr["result"] = 0;
                return $retArr;
            }
            $repair = $this->select('*')
                    ->where("cabinet_id",'=',$box->id)
                    ->where("client_id",'=',$user->client_id)
                    ->where("cell_id",'=',$cell->id)
                    ->where("is_delete",'=',0) //
                    ->where("status",'!=',2)
                    ->first();
            if($repair){
                if($repair->status == 0 || $repair->status == 1){
                    $retArr["msg"]= "设备已报修，请等待";
                    $retArr["result"] = 2;
                    return $retArr;
                }
            }
            $re = new Repair();
            if($user->is_bind ==2){
                $re->client_id = $user->client_id;
            }
            $re->user_id = $user->id; 
            $re->cabinet_id = $box->id;
            $re->cell_id = $cell->id;
            $re->type = 1;//
            $re->title = "出问题了!";
            $re->introduction = $text;
            $re->status = 0;
            $re->save();
            //7-16wyc
//            $cell->status = 2;
//            $cell->save();
            $retArr["msg"]= "设备报修成功~！";
            $retArr["result"] = 1;
            //报修完，修改格子的 所有订单状态：
            $orders = Orders::select('*')
                    ->where('cell_id','=',$cell->id)
                    ->where('cabinet_id','=',$box->id)
                    ->where('user_id','=',$user->id)
                    ->where('status','=','2')
                    ->where('type','=','1')
                    ->get();
            foreach ($orders as $key => $order) {
                $order->status = 1;
                $order->save();
            }
            return $retArr;
        }
        $retArr["msg"]= "设备不存在,请联系管理员!";
        $retArr["result"] = 0;
        return $retArr;
    }
    //监测设备状态
    public function getStatus() {
        
    }
    
}